using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using IGlobalist.Cms.Model;

namespace IGlobalist.Cms.DataAccess
{
    /// <summary>
    /// Implenment BaseDAO class, IObjectDAO class 
    /// </summary>
    public class ObjectDAO : BaseDAO, IObjectDAO
    {
        /// <summary>
        /// Convert list to CMSObject list
        /// </summary>
        /// <param name="list">List data</param>
        /// <returns>CMSObject list</returns>
        private IList<ICmsObject> ConvertList2ListCmsObject(IList list)
        {
            IList<ICmsObject> retList = new List<ICmsObject>();
            if (list != null)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    retList.Add((ICmsObject)list[i]);
                }
            }
            return retList;
        }

        #region IObjectDAO Members
        /// <summary>
        /// Get CMSObject by Id
        /// </summary>
        /// <param name="id">ID of CMSObject</param>
        /// <returns>CMSObject</returns>
        public ICmsObject GetObject(long id)
        {
            return (ICmsObject)ExecuteQueryForObject("GetObject", id);
        }

        /// <summary>
        /// Get CMSObject list by classId
        /// </summary>
        /// <param name="classID">Id of class</param>
        /// <returns>CMSObject list</returns>
        
        public IList<ICmsObject> GetObjectListByClassID(long classID)
        {
            IList<ICmsObject> list =ConvertList2ListCmsObject( ExecuteQueryForList("GetObjectListByClassID", classID));

            return list ;
        }
        /// <summary>
        /// Get top CMSObject list by classId
        /// </summary>
        /// <param name="classID">Id of class</param>
        /// <returns>CMSObject list</returns>
        public IList<ICmsObject> GetTopObjectListByClassID(long classID)
        {
            IList<ICmsObject> list = ConvertList2ListCmsObject( ExecuteQueryForList("GetTopObjectListByClassID", classID));

            return list;
        }
        /// <summary>
        /// Get top CMSObject list by classTypeId
        /// </summary>
        /// <param name="classTypeID">Id of classType</param>
        /// <returns>CMSObject list</returns>
        public IList<ICmsObject> GetTopObjectListByClassTypeID(short classTypeID)
        {
            IList<ICmsObject> list = ConvertList2ListCmsObject(ExecuteQueryForList("GetTopObjectListByClassTypeID", classTypeID));

            return list;
        }

        
        /// <summary>
        /// Get CMSObject list by classType Id
        /// </summary>
        /// <param name="classTypeID">Id of class type</param>
        /// <returns>CMSObject list</returns>
        public IList<ICmsObject> GetObjectListAllByClassTypeID(short classTypeID)
        {
            IList<ICmsObject> list = ConvertList2ListCmsObject(ExecuteQueryForList("GetObjectListAllByClassTypeID", classTypeID));

            return list;
        }

         /// <summary>
        /// Get CMSObject list by name
        /// </summary>
        /// <param name="classTypeID">Name of class type</param>
        /// <returns>CMSObject list</returns>
        public IList<ICmsObject> GetObjectListByname(string name)
        {
            IList<ICmsObject> list = ConvertList2ListCmsObject(ExecuteQueryForList("GetObjectListByname", name));

            return list;
        }


        
        /// <summary>
        /// Get CMSObject list by text
        /// </summary>
        /// <param name="search">Search string</param>
        /// <returns>CMSObject list</returns>
        public IList<ICmsObject> GetObjectListAllBySearch(string search)
        {
            IList<ICmsObject> list = ConvertList2ListCmsObject(ExecuteQueryForList("GetObjectListAllBySearch", search));

            return list;
        }

         /// <summary>
        /// Get CMSObject list by search
        /// </summary>
        /// <param name="search">Search string</param>
        /// <returns>CMSObject list</returns>
        public IList<ICmsObject> GetObjectListAllByAdvancedSearch(Hashtable search)
        {
            IList<ICmsObject> list = ConvertList2ListCmsObject(ExecuteQueryForList("GetObjectListAllByAdvancedSearch", search));

            return list;
        }
        

       /// <summary>
       /// Get CMSObject list all version by versionId
       /// </summary>
       /// <param name="firstVersionID">Id of version</param>
       /// <returns>CMSObject list</returns>
        public IList<ICmsObject> GetObjectAllVersion(long firstVersionID)
        {
            IList<ICmsObject> list = ConvertList2ListCmsObject(ExecuteQueryForList("GetObjectAllVersion", firstVersionID));
            return list;
        }


        /// <summary>
        /// Get CMSObject list relate by objectId and classTypeID
        /// </summary>
        /// <param name="relateObjectID">Id of Object</param>
        /// <param name="classTypeID">Id of classType</param>
        /// <returns>CMSObject list</returns>
        public IList<ICmsObject> GetObjectListRelateToObjectID(long relateObjectID, short classTypeID)
        {
            Hashtable parameters = new Hashtable();
            parameters["ObjectID"] = relateObjectID;
            parameters["ClassTypeID"] = classTypeID;
            IList<ICmsObject> list = ConvertList2ListCmsObject( ExecuteQueryForList("GetObjectListRelateToObjectID", parameters));
            return list;
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <param name="obj">CMSObject object</param>
        /// <returns>Id of CMSObject</returns>
        public long InsertObject(ICmsObject obj)
        {
            return (long) ExecuteInsert("InsertObject", obj);
        }
        public void InsertObjectRelation(long id1, long id2, short classTypeID)
        {
            Hashtable parameters = new Hashtable();
            parameters["Object1ID"] = id1;
            parameters["Object2ID"] = id2;
            parameters["ClassTypeID"] = classTypeID;
            ExecuteInsert("InsertObjectRelation", parameters);
        }
        /// <summary>
        /// Update CMSObject
        /// </summary>
        /// <param name="obj">CMSObject object</param>
        public void UpdateObject(ICmsObject obj)
        {
            ExecuteUpdate("UpdateObject", obj);
        }
        /// <summary>
        /// Delete CMSObject by id
        /// </summary>
        /// <param name="id">Id of CMSObject</param>
        public void DeleteObject(long id)
        {
            DeleteObjectRelation(id);
            ExecuteDelete("DeleteObject", id);            
        }
        /// <summary>
        /// Delete relation CMSObject and CMSObject
        /// </summary>
        /// <param name="id">Id of CMSobject</param>
        public void DeleteObjectRelation(long id)
        {
            ExecuteDelete("DeleteObjectRelation", id);
        }
        /// <summary>
        /// Delete relation CMSObject by parent CMSObject id  and sub CMSObject id
        /// </summary>
        /// <param name="parentID">ID of parent CMSObject</param>
        /// <param name="subID">Id of sub CMSObject</param>
        public void DeleteObjectOneRelation(long parentID, long subID)
        {
            Hashtable parameters = new Hashtable();
            parameters["Object1ID"] = parentID;
            parameters["Object2ID"] = subID;
            ExecuteDelete("DeleteObjectOneRelation", parameters);
        }

        /// <summary>
        /// Delete cmsObject all by versionId
        /// </summary>
        /// <param name="firstVersionID">Id of version</param>
        public void DeleteObjectAllVersion(long firstVersionID)
        {
            ExecuteDelete("DeleteObjectAllVersion", firstVersionID);
        }
        #endregion

    }
}
